package com.chen.webservice.Utils;

import com.chen.webservice.model.Order;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.chen.webservice.Utils.RuleConstants.OrderType.BUSINESS;
import static com.chen.webservice.Utils.RuleConstants.StyleType.WORD;

/**
 * @description: 规则103 文字风格节目不能相邻
 * @params:
 * @return:
 * @author: chenzhiwen
 * @dateTime: 2021/10/21 下午7:16
 */
@Service
public class Weak103Handler implements RuleHandler {
    private static final int factor = 1;//系数
    private static final int ruleNum = 103;

    @Override
    public int ruleNum() {
        return this.ruleNum;
    }

    @Override
    public boolean isMatch(List<Integer> rules) {
        return rules.contains(ruleNum);//强校验
    }

    @Override
    public int checkResult(List<Order> orders, int rate) {
        List<Order> filterOrders = orders.stream().filter(x -> !x.getCategory().equals(RuleConstants.OrderType.FIXED.getValue()))
                .collect(Collectors.toList());
        rate = 0;
        int len = filterOrders.size();
        if (len <= 1) {
            return rate;
        }
        for (int i = 0; i < len - 1; i++) {
            Order o1 = filterOrders.get(i);
            Order o2 = filterOrders.get(i + 1);
            if (o1.getStyle() == WORD.getValue() && o2.getStyle() == WORD.getValue()) {
                rate += 1;
            }//0
        }
        return rate * factor;
    }
}
